'use client'

import React from 'react';
import { BaseEditorProps } from './types';

export default function CarouselEditor({ properties, updateProperty, handleFileSelect }: BaseEditorProps) {
  return (
    <div className="space-y-4">
      <div>
        <h3 className="font-medium text-gray-800 mb-3">轮播项目</h3>
        <div className="space-y-3">
          {(properties.items || []).map((item: any, index: number) => (
            <div key={index} className="border border-gray-200 rounded p-3 bg-gray-50">
              <div className="space-y-2">
                <div>
                  <label className="block text-xs text-gray-600 mb-1">图片URL</label>
                  <input
                    type="text"
                    className="w-full p-2 border border-gray-300 rounded text-sm"
                    value={item.src || ""}
                    onChange={(e) => {
                      const newItems = [...(properties.items || [])];
                      newItems[index].src = e.target.value;
                      updateProperty('items', newItems);
                    }}
                  />
                </div>
                <div>
                  <label className="block text-xs text-gray-600 mb-1">上传图片</label>
                  <input
                    type="file"
                    accept="image/*"
                    onChange={(e) => handleFileSelect(e, { type: 'carousel', index })}
                    className="w-full text-sm file:mr-2 file:py-1 file:px-3 file:rounded file:border file:text-sm file:font-medium file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100"
                  />
                </div>
                <button
                  type="button"
                  className="text-red-500 hover:text-red-700 text-sm"
                  onClick={() => {
                    const newItems = [...(properties.items || [])];
                    newItems.splice(index, 1);
                    updateProperty('items', newItems);
                  }}
                >
                  删除此项目
                </button>
              </div>
            </div>
          ))}
          <button
            type="button"
            className="w-full py-2 border-2 border-dashed border-gray-300 rounded text-gray-500 hover:border-gray-400 hover:text-gray-600 text-sm"
            onClick={() => {
              const newItems = [...(properties.items || []), { src: "", type: "image", alt: "" }];
              updateProperty('items', newItems);
            }}
          >
            + 新增轮播项目
          </button>
        </div>
      </div>

      <div className="space-y-3">
        <div>
          <label className="block text-sm font-medium text-gray-700 mb-1">自动播放延迟 (毫秒)</label>
          <input
            type="number"
            className="w-full p-2 border border-gray-300 rounded"
            value={properties.autoplayDelay || 1000}
            onChange={(e) => updateProperty('autoplayDelay', parseInt(e.target.value))}
          />
        </div>
        <div>
          <label className="block text-sm font-medium text-gray-700 mb-1">宽度</label>
          <input
            type="text"
            className="w-full p-2 border border-gray-300 rounded"
            value={properties.width}
            onChange={(e) => updateProperty('width', e.target.value)}
          />
        </div>
      </div>

      <div className="flex items-center">
        <input
          type="checkbox"
          id="autoplay-checkbox"
          checked={properties.autoplay !== false}
          onChange={(e) => updateProperty('autoplay', e.target.checked)}
          className="mr-2 h-4 w-4 text-blue-600 rounded border-gray-300 focus:ring-blue-500"
        />
        <label htmlFor="autoplay-checkbox" className="text-sm font-medium text-gray-700">
          自动播放
        </label>
      </div>
    </div>
  );
}
